<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-sndhdr.html" />
<link rel="prev" href="module-rgbimg.html" />
<link rel="parent" href="mmedia.html" />
<link rel="next" href="module-sndhdr.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>19.9 imghdr -- Determine the type of an image</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="19.8 rgbimg  "
  href="module-rgbimg.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="19. multimedia Services"
  href="mmedia.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="19.10 sndhdr  "
  href="module-sndhdr.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-rgbimg.html">19.8 rgbimg  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="mmedia.html">19. Multimedia Services</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-sndhdr.html">19.10 sndhdr  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0021900000000000000000">
19.9 <tt class="module">imghdr</tt> --
         Determine the type of an image</a>
</h1>

<p>
<a name="module-imghdr"></a>

<p>
The <tt class="module">imghdr</tt> module determines the type of image contained in a
file or byte stream.

<p>
The <tt class="module">imghdr</tt> module defines the following function:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-4657' xml:id='l2h-4657' class="function">what</tt></b>(</nobr></td>
  <td><var>filename</var><big>[</big><var>, h</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Tests the image data contained in the file named by <var>filename</var>,
and returns a string describing the image type.  If optional <var>h</var>
is provided, the <var>filename</var> is ignored and <var>h</var> is assumed to
contain the byte stream to test.
</dl>

<p>
The following image types are recognized, as listed below with the
return value from <tt class="function">what()</tt>:

<p>
<div class="center"><table class="realtable">
  <thead>
    <tr>
      <th class="left"  >Value</th>
      <th class="left"  >Image format</th>
      </tr>
    </thead>
  <tbody>
    <tr><td class="left"   valign="baseline"><code>'rgb'</code></td>
        <td class="left"  >SGI ImgLib Files</td></tr>
    <tr><td class="left"   valign="baseline"><code>'gif'</code></td>
        <td class="left"  >GIF 87a and 89a Files</td></tr>
    <tr><td class="left"   valign="baseline"><code>'pbm'</code></td>
        <td class="left"  >Portable Bitmap Files</td></tr>
    <tr><td class="left"   valign="baseline"><code>'pgm'</code></td>
        <td class="left"  >Portable Graymap Files</td></tr>
    <tr><td class="left"   valign="baseline"><code>'ppm'</code></td>
        <td class="left"  >Portable Pixmap Files</td></tr>
    <tr><td class="left"   valign="baseline"><code>'tiff'</code></td>
        <td class="left"  >TIFF Files</td></tr>
    <tr><td class="left"   valign="baseline"><code>'rast'</code></td>
        <td class="left"  >Sun Raster Files</td></tr>
    <tr><td class="left"   valign="baseline"><code>'xbm'</code></td>
        <td class="left"  >X Bitmap Files</td></tr>
    <tr><td class="left"   valign="baseline"><code>'jpeg'</code></td>
        <td class="left"  >JPEG data in JFIF or Exif formats</td></tr>
    <tr><td class="left"   valign="baseline"><code>'bmp'</code></td>
        <td class="left"  >BMP files</td></tr>
    <tr><td class="left"   valign="baseline"><code>'png'</code></td>
        <td class="left"  >Portable Network Graphics</td></tr></tbody>
</table></div>

<p>

<span class="versionnote">New in version 2.5:
Exif detection.</span>

<p>
You can extend the list of file types <tt class="module">imghdr</tt> can recognize by
appending to this variable:

<p>
<dl><dt><b><tt id='l2h-4658' xml:id='l2h-4658'>tests</tt></b></dt>
<dd>
A list of functions performing the individual tests.  Each function
takes two arguments: the byte-stream and an open file-like object.
When <tt class="function">what()</tt> is called with a byte-stream, the file-like
object will be <code>None</code>.

<p>
The test function should return a string describing the image type if
the test succeeded, or <code>None</code> if it failed.
</dd></dl>

<p>
Example:

<p>
<div class="verbatim"><pre>
&gt;&gt;&gt; import imghdr
&gt;&gt;&gt; imghdr.what('/tmp/bass.gif')
'gif'
</pre></div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="19.8 rgbimg  "
  href="module-rgbimg.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="19. multimedia Services"
  href="mmedia.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="19.10 sndhdr  "
  href="module-sndhdr.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-rgbimg.html">19.8 rgbimg  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="mmedia.html">19. Multimedia Services</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-sndhdr.html">19.10 sndhdr  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
